package com.my.springbootexcel.controller;

import com.my.springbootexcel.bean.ExcelBean;
import com.my.springbootexcel.service.ExcelService;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @description:
 * @author: marshal
 * @date: 2021-08-03 4:12 PM
 */
@Controller
@RequestMapping("/excel")
public class ExcelController {

    @Autowired
    private ExcelService service;

    @GetMapping("/index")
    public String index(){
        return "views/index";
    }

    @GetMapping("/list")
    public String list(Model model) {
        List<ExcelBean> list = service.list();
        model.addAttribute("list", list);
        return "views/list";
    }




    /**
     *  导入excel，以Map的形式
     *
     * @param multipartFile
     * @return
     */
    @PostMapping("/importExcel")
    public String importExcel(@RequestParam("file") MultipartFile multipartFile) {
        service.importExcel(multipartFile);
        // 跳转到list页面
        return "redirect:/excel/list";
    }

    /**
     *  导入excel，以Bean的形式
     *
     * @param multipartFile
     * @return
     */
    @PostMapping("/importExcelByBean")
    public String importExcelByBean(@RequestParam("file") MultipartFile multipartFile) {
        service.importExcelByBean(multipartFile);
        // 跳转到list页面
        return "redirect:/excel/list";
    }

    /**
     *  导入excel，读取指定行
     *
     * @param multipartFile
     * @return
     */
    @PostMapping("/importExcelCustom")
    public String importExcelCustom(@RequestParam("file") MultipartFile multipartFile) {
        service.importExcelCustom(multipartFile);
        // 跳转到list页面
        return "redirect:/excel/list";
    }

    @RequestMapping("/downloadTemplate")
    public String downloadTemplate(HttpServletResponse response) {
        try {
            String fileName = "导入模板.xlsx";
            response.setCharacterEncoding("UTF-8");
            // 设置信息给客户端不解析
//            // 设置Content-type，即告诉客户端所发送的数据属于什么类型
            response.setHeader("content-Type", "application/vnd.ms-excel");
            // 将文件名称编码，否则会乱码
            Charset utf8 = StandardCharsets.UTF_8;
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName,StandardCharsets.UTF_8));

            // 输出文件
            ClassPathResource cpr = new ClassPathResource("/templates/excel/"+"excel_map.xlsx");
            InputStream is = cpr.getInputStream();
            ServletOutputStream outputStream = response.getOutputStream();
            byte[] buff = new byte[1024];
            BufferedInputStream bis = new BufferedInputStream(is);
            int i = bis.read(buff);
            while (i != -1) {
                outputStream.write(buff, 0, buff.length);
                outputStream.flush();
                i = bis.read(buff);
            }
            // 关闭资源
            bis.close();
            outputStream.close();
            is.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

}
